package org.jansansad.data;

import java.util.ArrayList;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;

import org.jansansad.client.Member;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.SortDirection;
import com.google.appengine.repackaged.com.google.common.base.Pair;

public class DataQuery {
	private DatastoreService datastore;
	
	public DataQuery() {
		datastore = DatastoreServiceFactory.getDatastoreService();
	}
	
	public List<String> ListConstituencies(String state) {
		Query q = new Query("Members");
		q.addSort("constituency",  SortDirection.ASCENDING);
		PreparedQuery pq = datastore.prepare(q);
		List<String> constituencies = new ArrayList<String>();
		for (Entity entity : pq.asIterable()) {
			String constituency = (String) entity.getProperty("constituency");
			String constituency_state = (String) entity.getProperty("state");
			if (constituency_state.equals(state)) {
				constituencies.add(constituency);
			}
		}
		return constituencies;
	}
	
	public SortedSet<String> ListStates() {
		Query q = new Query("Members");
		q.addSort("state", SortDirection.ASCENDING);
		PreparedQuery pq = datastore.prepare(q);
		SortedSet<String> states = new TreeSet<String>();
		for (Entity entity : pq.asIterable()) {
			String state = (String) entity.getProperty("state");
			states.add(state);
		}
		return states;
	}

	public List<Pair<String, String>> ListConstituencesAndStates() {
		Query q = new Query("Members");
		q.addSort("constituency",  SortDirection.ASCENDING);
		PreparedQuery pq = datastore.prepare(q);
		List<Pair<String, String>> constituency_state_pairs = new ArrayList<Pair<String, String>>();
		for (Entity entity : pq.asIterable()) {
			String constituency = (String) entity.getProperty("constituency");
			String state = (String) entity.getProperty("state");
			constituency_state_pairs.add(new Pair<String, String>(constituency, state));
		}
		return constituency_state_pairs;
	}
	
	public Member FindMemberByConstituencyAndState(String constituency, String state) {
		Query q = new Query("Members");
		q.addFilter("constituency", Query.FilterOperator.EQUAL, constituency);
		q.addFilter("state", Query.FilterOperator.EQUAL, state);
		PreparedQuery pq = datastore.prepare(q);
		Member member = null;
		if (pq.asIterable().iterator().hasNext()) {
		  Entity entity = pq.asIterable().iterator().next();
		  member = new Member();
		  member.firstName = (String) entity.getProperty("first_name");
		  member.lastName = (String) entity.getProperty("last_name");
		  member.constituencyName = (String) entity.getProperty("constituency");
		  member.stateName = (String) entity.getProperty("state");
		  member.partyName = (String) entity.getProperty("party");
		  member.emailIds = (String) entity.getProperty("email_ids");
		  member.permanentAddress = (String) entity.getProperty("permanent_address");
		  member.delhiAddress = (String) entity.getProperty("delhi_address");
		  member.telephoneNos = (String) entity.getProperty("telephone_nos");
		  member.delhiTelephoneNos = (String) entity.getProperty("delhi_telephone_nos");
		  member.imageUrl = (String) entity.getProperty("image_url");
	  }
	  return member;
	}
}
